import {
  Form
} from 'antd-mini/es/Form/form';
import {
  delayWorkOrderReviewSubmit,
  getDelayReview,
  ossDownload
} from '/service/service';

Page({
  data: {
    id: null,
    data: null,
    form: null,
    attachments: [],
    handleResultOptions: [{
        value: '审核通过',
        text: '审核通过'
      },
      {
        value: '审核拒绝',
        text: '审核拒绝'
      },
    ]
  },
  onLoad(option) {
    const id = option.id
    this.setData({
      id
    })
    this.form = new Form({
      initialValues: {},
      rules: {
        handleResult: [{
          required: true,
          message: '请选择处理结果',
        }],
        auditOpinion: [{
          required: true,
          message: '请输入审核意见',
        }, ],
      },
    })
    this.getInfo(id)
  },

  async getInfo(id) {
    try {
      const data = await getDelayReview(id)
      console.log('接口返回数据:', data)
      const {
        attachments
      } = data
      let imgList = []
      if (attachments && attachments.length > 0) {
        for (const s of attachments) {
          const params = {
            ...s,
            ossPath: await ossDownload(s.ossId)
          }
          imgList.push(params)
        }
      }

      console.log('[图：]', imgList)

      this.setData({
        data: {
          ...data,
          imgList,
          handleResult: null
        }
      })

      this.setFormValues(this.data.data)
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  setFormValues(data) {
    if (!this.form) return
    const formValues = {
      ...data
    }
    this.form.setFieldsValue(formValues)
  },

  handleRef(ref) {
    // console.log(ref.props.name)
    if (this.form && ref) {
      this.form.addItem(ref);

      // // 如果已有数据，立即同步到该字段
      // if (this.data.data) {
      //   const fieldName = ref.props.name
      //   this.form.setFieldsValue({
      //     [fieldName]: this.data.data[fieldName] || ''
      //   })
      // }
    }
  },
  async submit() {
    try {
      const values = await this.form.submit()

      const params = {
        ...values,
        id: this.data.id,
        attachments: this.data.attachments
      }

      console.log(params)
      const res = await delayWorkOrderReviewSubmit(params)
      console.log(res)
      my.navigateBack();
      if (res && res.code === 200) {
        my.showToast({
          content: res.msg,
          type: 'success',
          duration: 2000
        })
        my.navigateBack()
      } else {
        my.showToast({
          content: '提交失败，请检查',
          type: 'fail',
          duration: 2000
        })
      }
    } catch (e) {
      my.showToast({
        content: '提交失败，请检查',
        type: 'fail',
        duration: 2000
      })
    }
  },
  auditResultChange(e) {
    console.log(e)
    this.setData({
      auditResult: e
    })
  },
  cancel() {
    my.navigateBack();
  },
  async imagePreview(e) {
    const {
      url,
    } = e.currentTarget.dataset
    this.previewSingleImage(url)
  },

  previewSingleImage(url) {
    dd.previewImage({
      urls: [url],
      current: url,
      success: () => {
        console.log('图片预览成功');
      },
      fail: (err) => {
        console.error('图片预览失败', err);
        dd.showToast({
          content: '预览失败，请重试',
          type: 'fail',
          duration: 2000
        });
      }
    });
  },
});